<?php

namespace App\Http\Controllers;

use App\Admin;
use App\Article;
use App\Visit;
use App\label;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class api extends Controller
{
    public function getWords()
    {
        $label = DB::select("select sum(`articles`.`visit`) as weight,labels.name as text from `articles` inner join `labels` on `labels`.`articleId` = `articles`.`id` group by `labels`.`name`");
        return json_encode($label);
    }

    public function getDates()
    {
        $dates = DB::select("select distinct LEFT(created_at,7) as yam from articles order by created_at desc");
        $arr = array();
        foreach ($dates as $date) {
            substr($date->yam, 0, 7);
            array_push($arr, $date->yam);
        }
        return json_encode($arr);
    }

    public function getArtByDate(Request $request)
    {
        $date = $request->input("date");
        if ($date === "all") {
            $articles = Article::where("status", 1)->orderBy("created_at", "desc")->get();
        } else {
            $articles = Article::where([["status", 1], ["created_at", "like", $date . "%"]])->orderBy("created_at", "desc")->get();
        }
        foreach ($articles as $art) {
            $art["author"] = Admin::where("id", $art["AdminId"])->first()["name"];
        }

        return json_encode($articles);
    }

    public function getArts(Request $request)
    {
        $article = Article::where("status", 1)->orderBy("istop", "desc")->orderBy("created_at", "desc")->paginate(5);
        foreach ($article as $art) {
            $art["author"] = Admin::where("id", $art["AdminId"])->first()["name"];
        }
        echo json_encode($article);
    }

    public function getArt(Request $request)
    {
        $articleId = $request->input("id");
        $article = Article::where("id", $articleId)->first();

        if($article === null){
            return false;
        }

        if (getenv("HTTP_X_FORWARDED_FOR")) {
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        } else {
            $ip = $_SERVER["REMOTE_ADDR"];
        }


        $visit = Visit::where([
            ["ArticleId", $articleId],
            ["ip", $ip]])->first();
        if (isset($visit)) {
            $time = time() - strtotime($visit->created_at);
            if ($time > 3600) {
                $article->visit++;
                $article->save();
                $visit->delete();
            }
        } else {
            $visit = new Visit;
            $visit->ArticleId = $articleId;
            $visit->ip = $ip;
            $visit->save();
            $article->visit++;
            $article->save();
        }

        $article["author"] = Admin::where("id", $article["AdminId"])->first()["name"];
        return json_encode($article);
    }

    public function getLabel(Request $request)
    {
        $id = intval($request->input("id"));
        $label = Label::where("articleId", $id)->get();
        echo json_encode($label);
    }
}
